package com.cloudjan.codebuilder.util;

import com.cloudjan.codebuilder.entity.DBConfig;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 数据库工具类
 */
public class CodeBuilderUtils {

    private static Connection connection;

    public static Connection getConnection() {
        if(connection==null){
            throw new RuntimeException("数据库未初始化！");
        }
        return connection;
    }

    public static Connection init(DBConfig db) {
        if (connection == null) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(db.getUrl(), db.getUserName(), db.getPassword());
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

}
